import { renderAmis } from 'utils/amisUtil'

function Shop() {
  return renderAmis({
    type: 'page',
    body: [
      {
        type: 'crud',
        api: '/mct_shop/v1?page=$page&size=$perPage&mct_id=$mct_id&shop_name=$shop_name&cabinet_code=$cabinet_code&device_code=$device_code&distribution=$distribution&run=$run&mct_type=$mct_type',
        syncLocation: false,
        autoFillHeight: true,
        filter: {
          columnCount: 4,
          labelWidth: '70px',
          mode: 'horizontal',
          actions: [
            {
              type: 'reset',
              label: '重置'
            },
            {
              type: 'submit',
              label: '查询',
              level: 'primary'
            }
          ],
          body: [
            {
              type: 'select',
              label: '选择商户',
              name: 'mct_id',
              autoComplete: {
                url: '/mct/selected/v1?page=1&size=10&mct_type=0&short_or_full_name=$term',
                responseData: {
                  options: '${rows|pick:label~full_name,value~id}'
                }
              },
              placeholder: '请选择商户',
              searchable: true
            },
            {
              type: 'input-text',
              label: '店铺名',
              name: 'shop_name',
              placeholder: '请输入店铺名'
            },
            {
              type: 'input-text',
              label: '货柜编号',
              name: 'cabinet_code',
              placeholder: '请输入货柜编号'
            },
            {
              type: 'input-text',
              label: '设备编号',
              name: 'device_code',
              placeholder: '请输入设备编号'
            },
            {
              type: 'select',
              name: 'distribution',
              label: '分配状态',
              options: [
                {
                  label: '未分配',
                  value: '0'
                },
                {
                  label: '已分配',
                  value: '1'
                }
              ],
              placeholder: '请选择分配状态'
            },
            {
              type: 'select',
              name: 'run',
              label: '运行状态',
              options: [
                {
                  label: '离线',
                  value: '0'
                },
                {
                  label: '在线',
                  value: '1'
                }
              ],
              placeholder: '请选择运行状态'
            },
            {
              type: 'select',
              name: 'mct_type',
              label: '业务类别',
              options: [
                {
                  label: '运营商',
                  value: '0'
                },
                {
                  label: '服务商',
                  value: '1'
                }
              ],
              placeholder: '请选择业务类别'
            }
          ]
        },
        headerToolbar: [
          {
            type: 'action',
            align: 'right',
            label: '导出',
            actionType: 'ajax',
            api: 'get:/export/mct_shop/v1?mct_id=${mct_id}&shop_name=${shop_name}&cabinet_code=${cabinet_code}&device_code=${device_code}&distribution=${distribution}&run=${run}&mct_type=${mct_type}'
          },
          {
            type: 'columns-toggler',
            align: 'right',
            draggable: true,
            icon: 'fas fa-cog',
            overlay: true,
            footerBtnSize: 'sm'
          }
        ],
        columns: [
          {
            name: 'id',
            label: '店铺Id',
            fixed: 'left'
          },
          {
            label: '商户名称',
            name: 'mct_name',
            width: 180,
            type: 'tpl',
            tpl: '${mct_name|truncate:20}',
            popOver: {
              trigger: 'hover',
              position: 'left-top',
              showIcon: false,
              body: {
                type: 'tpl',
                tpl: '${mct_name}'
              }
            }
          },
          {
            name: 'pre_mct_name',
            label: '上级商户名称',
            width: 180,
            type: 'tpl',
            tpl: '${pre_mct_name|truncate:20}',
            popOver: {
              trigger: 'hover',
              position: 'left-top',
              showIcon: false,
              body: {
                type: 'tpl',
                tpl: '${pre_mct_name}'
              }
            }
          },
          {
            name: 'mct_type',
            label: '业务类别'
          },
          {
            name: 'shop_name',
            label: '店铺名称'
          },
          {
            name: 'cabinet_code',
            label: '货柜编号'
          },
          {
            name: 'device_code',
            label: '设备编号'
          },
          {
            name: 'cabinet_model',
            label: '设备型号',
            width: 110
          },
          {
            name: 'enabled',
            label: '店铺状态',
            type: 'static-mapping',
            map: {
              0: "<span style='color: #f5222d'>禁用</span>",
              1: "<span style='color: #52c41a'>启用</span>"
            },
            value: '${enabled}'
          },
          {
            name: 'run',
            label: '运行状态',
            type: 'static-mapping',
            map: {
              0: "<span style='color: #f5222d'>离线</span>",
              1: "<span style='color: #52c41a'>在线</span>"
            },
            value: '${enabled}'
          },
          {
            name: 'power',
            type: 'static-mapping',
            map: {
              0: "<span style='color: #f5222d'>电池</span>",
              1: "<span style='color: #52c41a'>市电</span>"
            },
            label: '供电状态'
          },
          {
            name: 'act_stock',
            label: '剩余库存'
          },
          {
            name: 'create_time',
            label: '创建时间',
            width: 130
          },
          {
            type: 'operation',
            label: '操作',
            width: 180,
            fixed: 'right',
            buttons: [
              {
                type: 'button-group',
                buttons: [
                  {
                    type: 'button',
                    label: '查看模板',
                    level: 'link',
                    actionType: 'dialog',
                    dialog: {
                      title: '查看模板',
                      actions: [],
                      body: {
                        type: 'page',
                        initApi: {
                          method: 'get',
                          url: '$API_HOST/mct_shop/template/$id/v1',
                          responseData: {
                            items: '${template}'
                          }
                        },
                        body: {
                          type: 'table',
                          style: {
                            height: '500px',
                            'overflow-y': 'auto'
                          },
                          columns: [
                            {
                              name: 'logo_url',
                              label: '商品图片',
                              type: 'image',
                              src: '$logo_url',
                              width: '30px',
                              height: '30px',
                              enlargeAble: true,
                              thumbMode: 'contain'
                            },
                            {
                              name: 'sensor',
                              label: '货道编号'
                            },
                            {
                              name: 'weight',
                              label: '重量',
                              type: 'tpl',
                              tpl: '${weight}g'
                            },
                            {
                              name: 'retail_price',
                              label: '单价',
                              type: 'tpl',
                              tpl: '${(retail_price/100)}'
                            }
                          ]
                        }
                      }
                    }
                  },
                  {
                    type: 'button',
                    label: '下载日志',
                    level: 'link',
                    actionType: 'dialog',
                    dialog: {
                      title: '下载日志',
                      body: {
                        type: 'form',
                        api: {
                          method: 'put',
                          url: '$API_HOST/device/log/${deviceCode}/v1',
                          data: {
                            date: '$date'
                          },
                          requestAdaptor:
                            "var date = api.data.date.split(','); var format = 'YYYY-MM-DD HH:mm:ss'; return {...api, data: {start_date: dayjs(Number(date[0])).format(format)}}"
                        },
                        body: [
                          {
                            type: 'input-date-range',
                            name: 'date',
                            label: '开始结束时间',
                            required: true
                          }
                        ]
                      }
                    }
                  },
                  {
                    type: 'button',
                    label: '详情',
                    level: 'link',
                    actionType: 'link',
                    link: '/container-center/shop-manager/$id'
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  })
}

export default Shop
